/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserServlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import UserManager.*;
/**
 *
 * @author dizonfarleyjr
 */
@WebServlet(name = "registerServlet", urlPatterns = {"/registerServlet"})
public class registerServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet registerServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet registerServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
            
            User newUser = new User();
            CreditCard newCard = new CreditCard();
            
            newUser.setUserType("Customer");
            newUser.setNameFirst(request.getParameter("nameFirst"));
            newUser.setNameMiddleI(request.getParameter("nameMiddleI"));
            newUser.setNameLast(request.getParameter("nameLast"));
            newUser.setTelephone(Integer.parseInt(request.getParameter("telephone")));
            newUser.setUsername(request.getParameter("username"));
            newUser.setPassword(request.getParameter("password"));
            newUser.setEmail(request.getParameter("email"));
            newUser.setIsActive(5);
            newUser.setCreditNumber(request.getParameter("creditNumber"));
            newUser.setAddressDeliveryNumber(request.getParameter("AddressDeliveryNumber"));
            newUser.setAddressDeliveryStreet(request.getParameter("AddressDeliveryStreet"));
            newUser.setAddressDeliverySubdivision(request.getParameter("AddressDeliverySubdivision"));
            newUser.setAddressDeliveryCity(request.getParameter("AddressDeliveryCity"));
            newUser.setAddressDeliveryPostal(request.getParameter("AddressDeliveryPostal"));
            newUser.setAddressDeliveryCountry(request.getParameter("AddressDeliveryCountry"));
            
            
            
            newCard.setCreditNumber(request.getParameter("creditNumber"));
            newCard.setCreditName(request.getParameter("creditName"));
            newCard.setCreditMonth(Integer.parseInt(request.getParameter("creditMonth")));
            newCard.setCreditYear(Integer.parseInt(request.getParameter("creditYear")));
            newCard.setCreditCompany(request.getParameter("creditCompany"));
            newCard.setAddressBillNumber(request.getParameter("addressBillNumber"));
            newCard.setAddressBillStreet(request.getParameter("addressBillStreet"));
            newCard.setAddressBillSubdivision(request.getParameter("addressBillSubdivision"));
            newCard.setAddressBillCity(request.getParameter("addressBillCity"));
            newCard.setAddressBillPostal(request.getParameter("addressBillPostal"));
            newCard.setAddressBillCountry(request.getParameter("addressBillCountry"));
            
           boolean result = new UserManager().addUser(newUser, newCard);
            if (result==true)
                response.sendRedirect("index.jsp");
            else
                response.sendRedirect("register.jsp");
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
